<template>
	<xn-form-container
		:title="formData.id ? '编辑文体社团' : '增加文体社团'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="社团名称：" name="groupName">
				<a-input v-model:value="formData.groupName" placeholder="请输入社团名称" allow-clear />
			</a-form-item>
			<a-form-item label="社团标签：" name="groupTag">
				<a-input v-model:value="formData.groupTag" placeholder="请输入社团标签" allow-clear />
			</a-form-item>
			<a-form-item label="社团分类：" name="groupCate">
				<a-select
					v-model:value="formData.groupCate"
					:options="cateOptions"
					class="xn-wd"
					placeholder="请选择社团分类"
				/>
			</a-form-item>
			<a-form-item label="指定管理人员：" name="managerId">
				<xn-user-selector
					:org-tree-api="selectorApiFunction.orgTreeApi"
					:user-page-api="selectorApiFunction.userPageApi"
					:user-list-by-id-list-api="selectorApiFunction.checkedUserListApi"
					:radio-model="true"
					v-model:value="formData.managerId"
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizGroupForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizGroupApi from '@/api/biz/bizGroupApi'
	import bizOrgApi from '@/api/biz/bizOrgApi'
	import userCenterApi from '@/api/sys/userCenterApi'
	import { globalStore } from '@/store'
	import tool from '@/utils/tool'

	const store = globalStore()
	// 获取分类字典
	const cateOptions = tool.dictList('GROUP_CATE')
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	// 搜索框数据
	const userOption = ref([])
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		groupName: [required('请输入社团名称')],
		managerId: [required('请输入管理员id')],
		groupCate: [required('请输入社团分类')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		const userInfo = store.userInfo
		console.log(userInfo.valueOf())
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				formDataParam.managerId = userInfo.valueOf().id
				formDataParam.managerName = userInfo.valueOf().name
				bizGroupApi
					.bizGroupSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}

	// 传递设计器需要的API
	const selectorApiFunction = {
		orgTreeApi: (param) => {
			return bizOrgApi.orgTreeSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		},
		userPageApi: (param) => {
			return bizOrgApi.orgUserSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		},
		checkedUserListApi: (param) => {
			return userCenterApi.userCenterGetUserListByIdList(param).then((data) => {
				return Promise.resolve(data)
			})
		}
	}
	onMounted(() => {})
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
